#!/usr/bin/env python
# -*- coding: utf-8 -*-
import random
import json
import index
import mysql.connector
import config

# 打开数据库连接
db = mysql.connector.connect(host=config.c("mysql","host"), port=config.c("mysql","port"), user=config.c("mysql","user"), passwd=config.c("mysql","passwd"), db=config.c("mysql","db"), charset=config.c("mysql","charset"))

# 使用cursor()方法获取操作游标 
cursor = db.cursor()

def wl_query(sql):
# SQL 查询语句

    try:
    # 执行SQL语句
        cursor.execute(sql)

        # 获取所有记录列表
        results = cursor.fetchall()
        
        arr=[]

        for row in results:
            id = row[0]
            name = row[1]
            my_arr={}
            # 打印结果
            # print "id=%s,name=%s" % \
            #         (id, name)
            my_arr['company_id']=id
            my_arr['company_name']=name

            arr.append(my_arr)

        
        return arr
    except:
        print "Error: unable to fecth data"
        index.log(sql)

    db.close()


